#include "Renderer_PCH.h"
#include "RDepthstencilState.h"

/*--------------------------------------------------------------------------*/

RDepthstencilState::RDepthstencilState() :
	m_depthTest(GL_TRUE),
	m_depthMask(GL_TRUE),
	m_depthFunc(GL_LEQUAL),
	m_stencilTest(GL_TRUE),
	m_stencilMask(GL_TRUE)
{

}

/*--------------------------------------------------------------------------*/

GLenum RDepthstencilState::GetDepthTest() const
{
	return m_depthTest;
}

/*--------------------------------------------------------------------------*/

GLenum RDepthstencilState::GetDepthMask() const
{
	return m_depthMask;
}

/*--------------------------------------------------------------------------*/

GLenum RDepthstencilState::GetDepthFunc() const
{
	return m_depthFunc;
}

/*--------------------------------------------------------------------------*/

GLenum RDepthstencilState::GetStecilTest() const
{
	return m_stencilTest;
}

/*--------------------------------------------------------------------------*/

GLenum RDepthstencilState::GetStencilMask() const
{
	return m_stencilMask;
}

/*--------------------------------------------------------------------------*/

void RDepthstencilState::SetDepthTest(GLenum n)
{
#ifdef _DEBUG
	__Check(n == GL_TRUE || n == GL_FALSE);
#endif
	m_depthTest = n;
}

/*--------------------------------------------------------------------------*/

void RDepthstencilState::SetDepthMask(GLenum n)
{
#ifdef _DEBUG
	__Check(n == GL_TRUE || n == GL_FALSE);
#endif
	m_depthMask = n;
}

/*--------------------------------------------------------------------------*/

void RDepthstencilState::SetDepthFunc(GLenum n)
{
#ifdef _DEBUG

#endif
	m_depthFunc = n;
}

/*--------------------------------------------------------------------------*/

void RDepthstencilState::SetStencilTest(GLenum n)
{
#ifdef _DEBUG
	__Check(n == GL_TRUE || n == GL_FALSE);
#endif
	m_stencilTest = n;
}

/*--------------------------------------------------------------------------*/

void RDepthstencilState::SetStencilMask(GLenum n)
{
#ifdef _DEBUG
	__Check(n == GL_TRUE || n == GL_FALSE);
#endif
	m_stencilMask = n;
}

/*--------------------------------------------------------------------------*/

void RDepthstencilState::Bind() const
{
	if (m_depthTest == GL_TRUE)
	{
		glEnable(GL_DEPTH_TEST);
		glDepthMask(m_depthMask);
		glDepthFunc(m_depthFunc);
	}
	else
	{
		glDisable(GL_DEPTH_TEST);
	}

	if (m_stencilTest == GL_TRUE)
	{
		glEnable(GL_STENCIL_TEST);
		glStencilMask(m_stencilMask ? 0xff : 0x00);
	}
	else
	{
		glDisable(GL_STENCIL_TEST);
	}
}

/*--------------------------------------------------------------------------*/